package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.entity.MmallOrderItem;
import com.mall.service.MmallOrderItemService;
import com.mall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ppmall
 * @since 2021-09-19
 */
@RestController
@RequestMapping("/mall/orderItem")
public class MmallOrderItemController {

    @Autowired
    MmallOrderItemService orderItemService;

    @GetMapping("/orderItems/{orderNo}")
    public R getOrderItemByOrderNo(@PathVariable("orderNo") Long orderNo) {
        QueryWrapper<MmallOrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        List<MmallOrderItem> orderItems = orderItemService.list(wrapper);
        return R.ok().data("orderItems", orderItems);
    }


    @GetMapping("/orderItem")
    public R getOrderItemById(Integer id){
        MmallOrderItem orderItem = orderItemService.getById(id);
        return R.ok().data("orderItem", orderItem);
    }

    @PutMapping("/orderItem")
    public R updateOrderItemById(@RequestBody MmallOrderItem orderItem) {
        return orderItemService.updateById(orderItem) ? R.ok() : R.error();
    }

    @PostMapping("/orderItem")
    public R addOrderItem(@RequestBody MmallOrderItem orderItem) {
        System.out.println(orderItem);
        return orderItemService.save(orderItem) ? R.ok() : R.error();
    }

    @DeleteMapping("/orderItem/{id}")
    public R deleteOrderItemById(@PathVariable("id") Integer id){
        return orderItemService.removeById(id) ? R.ok() : R.error();
    }
}

